﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;

namespace NLLSite.Administrator.UserControl
{
    public partial class AddNewAdmin : System.Web.UI.UserControl
    {
        Admin ads = new Admin();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtTen.Text = string.Empty;
            txtTaiKhoan.Text = string.Empty;
            txtMatKhau.Text = string.Empty;
            txtDienThoai.Text = string.Empty;
            txtEmail.Text = string.Empty;
            CheckBox1.Checked = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            bool status = false;
            if (CheckBox1.Checked)
            {
                status = true;
            }
            else
            {
                status = false;
            }
            DAL_Class.Administrator ad = new DAL_Class.Administrator();
            ad.Username = txtTaiKhoan.Text;
            ad.Password = GetMD5Hash(txtMatKhau.Text);
            ad.Role = "Admin";
            ad.FullName = txtTen.Text;
            ad.Phone = txtDienThoai.Text;
            ad.Email = txtEmail.Text;
            ad.Status = status;
            if(ads.AddNewAdmin(ad))
                Response.Redirect("QuanLyAdmin.aspx");
            else
                Label1.Text = "Tài khoản đã tồn tại.";
            //lblSuccess.Visible = true;
           
        }
        public static string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }
    }
}